﻿// Copyright 2012 Jack Bulan (agentgeo)
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using GemWorkshop.Formats.Nitro;
using Be.Windows.Forms;

namespace GemWorkshop.Map
{
    public partial class MapSelector : Form
    {
        private MapEditor mapEd;
        private Narc matNarc;
        private MatrixLoader matLoader;

        private Font infoFont = new Font("Courier New", 8.0f, FontStyle.Regular);
        
        public MapSelector()
        {
            InitializeComponent();

            matNarc = new Narc(Form1.GameDirectory + "\\" + Form1.GameSettings.ReadValue(Form1.Game, "MatrixData"));
            //for (int i = 0; i < matNarc.FileCount; i++)
            //{
            //    listBox1.Items.Add(string.Format("Matrix {0}", i));
            //}
            matLoader = new MatrixLoader(matNarc);
            matLoader.Load(0); // The known world map matrix

            //mapEd = new MapEditor(matLoader);
            //mapEd.FormClosed += new FormClosedEventHandler(MapEd_Closed);
            string[] mapNames = MapLoader.LoadMapNames();
            //listBox1.Items.AddRange(mapNames);
            DataTable headers = new DataTable();
            headers.Columns.Add("Number").ReadOnly = true;
            headers.Columns.Add("Internal Name");
            for (int i = 0; i < mapNames.Length; i++)
            {
                var row = headers.NewRow();
                row["Number"] = i.ToString();
                row["Internal Name"] = mapNames[i];
                headers.Rows.Add(row);
            }
            dataGridView1.DataSource = headers;
        }

        private void MapSelector_Load(object sender, EventArgs e)
        {

        }

        private void bSave_Click(object sender, EventArgs e)
        {

        }

        private void bCross_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void MapEd_Closed(object sender, EventArgs e)
        {
            MessageBox.Show("Closed!\nRe-creating...");
        }

        private void pHeaders_Paint(object sender, PaintEventArgs e)
        {
            for (int y = 0; y < matLoader.header.Height; y++)
            {
                for (int x = 0; x < matLoader.header.Width; x++)
                {
                    ushort header = matLoader.Layer1[x, y];
                    if (header == 0)
                    {
                        if (matLoader.Layer2[x, y] > 0)
                        {
                            e.Graphics.FillRectangle(Brushes.Purple, x * 32, y * 16, 32, 16);
                        }
                        else
                        {
                            e.Graphics.FillRectangle(Brushes.Black, x * 32, y * 16, 32, 16);
                        }
                    }
                    else
                    {
                        e.Graphics.FillRectangle(Brushes.White, x * 32, y * 16, 32, 16);
                        e.Graphics.DrawString(header.ToString("0000"), infoFont, Brushes.Black, x * 32, y * 16);
                    }
                }
            }
        }

        private void pHeaders_MouseMove(object sender, MouseEventArgs e)
        {
            int x = e.X / 32;
            int y = e.Y / 16;
            lblXY.Text = string.Format("X: {0}, Y: {1}", x, y);

            
        }

        private void pHeaders_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int x = e.X / 32;
            int y = e.Y / 16;

            if (matLoader.Layer1[x, y] > 0)
            {
                mapEd = new MapEditor(matLoader, matLoader.Layer1[x, y], matLoader.Layer3[x, y]);
                mapEd.ShowDialog();
            }
        }
    }
}
